﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TSqlServer;

public partial class system_listmans_pagebar : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
        }
        else
        {
            string inman = Request["uid"].ToString();
            string pagenum = Request["pagenum"].ToString();
            pagebar.Text = GenPagebar(inman,pagenum);
        }
    }

    private string GenPagebar(string inman,string pagenum)
    {
        string result = "";

        int pageno = Convert.ToInt32(pagenum);
        int pagecounts = PageCount(inman);

        if (pagecounts > 1)
        {
            if(pageno==1)//第一页
            {
                result += "<span class=\"pagebox_num_nonce\"><a>首页</a></span>";
                for (int i = 1; i <= pagecounts; i++)
                {
                    if (i != pageno)
                    {
                        result += "<span class=\"pagebox_num\"><a onclick=\"getlinkmans('" + i.ToString() + "')\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>"+i.ToString()+"</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre_nolink\">上一页</span>";
                result += "<span class=\"pagebox_next\"><a onclick=\"getlinkmans('2')\">下一页</a></span>";
                result += "<span class=\"pagebox_num\"><a onclick=\"getlinkmans('" + pagecounts.ToString() + "')\">尾页</a></span>";
            }
            if((pageno>1)&&(pageno<pagecounts))
            {
                result += "<span class=\"pagebox_num\"><a onclick=\"getlinkmans('1')\">首页</a></span>";
                for (int i = 1; i <= pagecounts; i++)
                {
                    if (i != pageno)
                    {
                        result += "<span class=\"pagebox_num\"><a onclick=\"getlinkmans('" + i.ToString() + "')\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre\"><a onclick=\"getlinkmans('" + (pageno - 1).ToString() + "')\">上一页</a></span>";
                result += "<span class=\"pagebox_next\"><a onclick=\"getlinkmans('"+(pageno + 1).ToString()+"')\">下一页</a></span>";
                result += "<span class=\"pagebox_num\"><a onclick=\"getlinkmans('" + pagecounts.ToString() + "')\">尾页</a></span>";
            }
            
            if (pageno == pagecounts)//最后一页
            {
                result += "<span class=\"pagebox_num\"><a onclick=\"getlinkmans('1')\">首页</a></span>";
                for (int i = 1; i <= pagecounts; i++)
                {
                    if (i != pageno)
                    {
                        result += "<span class=\"pagebox_num\"><a onclick=\"getlinkmans('" + i.ToString() + "')\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre\"><a onclick=\"getlinkmans('"+(pagecounts-1).ToString()+"')\">上一页</a></span>";
                result += "<span class=\"pagebox_next_nolink\">下一页</span>";
                result += "<span class=\"pagebox_num_nonce\"><a>尾页</a></span>";
            }
        }
        else
        {
            result += "<span class=\"pagebox_num_nonce\"><a>首页</a></span>";
            result += "<span class=\"pagebox_num_nonce\"><a>1</a></span>";
            result += "<span class=\"pagebox_pre_nolink\">上一页</span>";
            result += "<span class=\"pagebox_next_nolink\">下一页</span>";
            result += "<span class=\"pagebox_num_nonce\"><a>尾页</a></span>";
        }
        
        return result;
    }

    private int PageCount(string inman)
    {
        int PageCounts = 0;

        int PageSize = 40;

        int RowCounts = 0;
        string sSql = "select count(*) from t_linkman where inman='" + inman + "'";
        RowCounts = TSqlDB.ExecuteScalar(sSql);

        if (RowCounts % PageSize == 0)  //余数为零
        {
            PageCounts = RowCounts / PageSize;
        }
        else
        {
            PageCounts = (int)(RowCounts / PageSize) + 1;
        }

        return PageCounts;
    }
}
